
##Function to Recode 0 to 1
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

### Neuroticism

neuroticism <- mapply(as.numeric,with(wvs_sub,data.frame(
  neu1,
  neu2,
  neu3, 
  neu4, 
  neu5, 
  neu6, 
  neu7, 
  neu8, 
  neu9,
  neu10)))


neuroticismlist <- list()
a <- 0
for(i in 1:9){
  a <- a+(ncol(combn(1:10, i)))
}

b <- 0
items <- list()
for(j in 1:9){
  b=b+1 
  combos <- combn(1:10, j)
  for(i in 1:ncol(combos)){
    items[[b]] <- combos
  }}

itemrow <- lapply(items,nrow)
itemcol <- lapply(items,ncol)

collapsepastecolumns <- function(x)apply(x,2,paste,collapse=",")
rr <- lapply(collapsepastecolumns,X = items)

numbero <-   unlist(itemcol)
emptylist <- list()  
for(i in 2:9){
  emptylist[[i-1]] <- rep(i,numbero[[i-1]])
}

h=1
j=2
i=1
alphacoefs <- data.frame(matrix(nrow=1012,ncol=4))

for(j in 2:9){
  combos <- combn(1:10, j)
  two <- matrix(nrow=nrow(wvs_sub),ncol=ncol(combos))
  for(i in 1:ncol(combos)){
    two[,i] <-   rowMeans(neuroticism[,combos[,i]])
    alphacoefs[h,] <- psych::alpha(neuroticism[,combos[,i]],check.keys = TRUE)$total[1:4]
    h=  h+1
  }
  lmcoefs <- matrix(nrow=ncol(combos),ncol=3)
  for(i in 1:ncol(two)){
    lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
    lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
    lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  }
  neuroticismlist[[j]] <- lmcoefs
  
}
lmcoefs <- matrix(ncol=3)
## 10 item 
lmcoefs[1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(rowMeans(neuroticism))+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
lmcoefs[2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(rowMeans(neuroticism))+  wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
lmcoefs[3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(rowMeans(neuroticism))+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]

neuroticismlist[[10]] <- lmcoefs
alphacoefs[1013,] <- psych::alpha(neuroticism,check.keys = TRUE)$total[1:4]


lmcoefs <- matrix(nrow=10,ncol=3)

for(i in 1:ncol(neuroticism)){
  lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1((neuroticism)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1((neuroticism)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
  lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1((neuroticism)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_ext+wvs_sub$IPIP_agre+wvs_sub$IPIP_open+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+  wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
}
neuroticismlist[[1]] <- lmcoefs

l <- Map(cbind, neuroticismlist, indx = seq_len(length(neuroticismlist))) 
h <- data.frame(do.call(rbind, l))
head(h )
names(h) <- c("Left-Right","Moral","Economic Ideology","Index")
empties <- data.frame(matrix(NA,nrow = 10,ncol = 4))
colnames(empties) <- colnames(alphacoefs)
alphacoefs <- rbind(empties,alphacoefs)
itemstogether <- unlist(rr)
itemstogether[1023] <- "all"
numall <- unlist(emptylist)
outstanding <- data.frame(h,itemstogether,alphacoefs)
library(reshape2)
forfig <- melt(outstanding,id.vars=c("Index","itemstogether",names(empties)))
forfig$trait <- "Neuroticism"
